package edu.whu.assignment6.service.impl;

import edu.whu.assignment6.dataobject.Product;
import edu.whu.assignment6.dataobject.Supplier;
import edu.whu.assignment6.mapper.ProductMapper;
import edu.whu.assignment6.mapper.SupplierMapper;
import edu.whu.assignment6.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Supplier findById(int sid) {
        return supplierMapper.selectById(sid);
    }

    @Override
    public int addSupplier(String sname, int pid) {
        Product exist = productMapper.selectById(pid);
        if(exist == null) {
            return -1;
        }
        Supplier supplier = new Supplier();
        supplier.setSname(sname);
        supplier.setPid(pid);
        return supplierMapper.insert(supplier);
    }

    @Override
    public int deleteSupplier(int sid) {
        return supplierMapper.deleteById(sid);
    }
}
